<?php


namespace App\Models;


use App\Traits\Model\DateTimeModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use LTools\Libraries\RedisUnique;

/**
 * Class Entryorder
 * @property Collection $products
 * @package App\Models
 * @author luffyzhao@vip.126.com
 */
class Entryorder extends Model
{
    use DateTimeModel;

    protected $fillable = [
        'code', 'foreign', 'expect_at', 'customer_id'
    ];


    protected static function boot()
    {
        parent::boot();
        static::registerModelEvent('creating', function (Entryorder $model){
            $key = sprintf("E%s", date('ymd'));
            $model->setAttribute('code', unique_code($key, 12, 10));
        });
    }
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function products(){
        return $this->hasMany(EntryorderProduct::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * @author luffyzhao@vip.126.com
     */
    public function logs(){
        return $this->hasMany(EntryorderLog::class);
    }

    /**
     * @return bool
     * @author luffyzhao@vip.126.com
     */
    public function isDraft(){
        return $this->getAttribute('status') === '0';
    }
}
